package vg.modules.util_plugins;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Observable;
import java.util.Observer;

import javax.swing.ImageIcon;
import javax.swing.JComponent;
import javax.swing.JToggleButton;
import javax.swing.JToolBar;
import javax.swing.SwingUtilities;

import vg.services.graph_view_manager.interfaces.graph_view.AGraphView;
import vg.services.graph_view_manager.interfaces.tab.ATab;
import vg.services.plugin_manager.event_and_request.event.AUIEvent;
import vg.services.plugin_manager.event_and_request.event.UIEventChangeTab;
import vg.services.plugin_manager.interfaces.IPlugin;
import vg.services.plugin_manager.realization.PluginParameter;
import vg.services.user_interface_manager.interfaces.AUserInterfaceElement;

public class PanningButtonPlugin extends AUserInterfaceElement implements IPlugin {
	// Main data
	private PluginParameter param;
	private AGraphView view;
	private JToolBar toolBar;
	private Observer observer;
	private JToggleButton button;
	
	// Mutex
	private final Object generalMutex = new Object();
	
	public PanningButtonPlugin() {
		super("PanningButtonPlugin", null);
		this.observer = new Observer() {
			public void update(Observable o, Object arg) {
			}
		};
	}
	
	@Override
	public void install(PluginParameter parameter) throws Exception {
		this.param = parameter;
		
		view  = null;
		button = new JToggleButton(new ImageIcon("./data/resources/textures/drag_hand.png"));
		button.setToolTipText("Panning graph view");
		
		button.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				synchronized (generalMutex) {
					if (view == null)
						return;
					view.getHighLevelGraphEditor().setPanning(button.isSelected());					
				}
			}
		});
		toolBar = new JToolBar();
		toolBar.setFloatable(false);
		toolBar.add(button);
		param.userInterface.addInstrument(this);
	}
	
	@Override
	public void update(Observable o, Object arg) {
		if (arg instanceof AUIEvent) {
			AUIEvent event = (AUIEvent) arg;
			switch (event.getType()) {
				case DEF_CHANGE_UI_STYLE: {
					SwingUtilities.invokeLater(new Runnable() {
						public void run() {
							synchronized (generalMutex) {
								SwingUtilities.updateComponentTreeUI(toolBar);
							}
						}
					});
					break;
				}
				case DEF_CHANGE_TAB: {
					synchronized (generalMutex) {
						if (view != null) {
							view.getGraphObservable().removeObserver(observer);
						}
						UIEventChangeTab bufEvent = (UIEventChangeTab) event;
						ATab tab = bufEvent.getTab();
						if (tab != null) {
							switch (tab.getTabType()) {
								case ATab.DEF_GRAPH_VIEW_TAB_TYPE: {
									view = (AGraphView)tab;
									if (view != null) {
										view.getGraphObservable().addObserver(observer);
										view.getHighLevelGraphEditor().setPanning(button.isSelected());
									}
									break;
								}							
							}
						}
					}
					break;
				}
				default:break;
			}
		}

	}
	
	@Override
	public JComponent getView() {
		return toolBar;
	}

}
